package com.example.rabbitmqspringboot.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zfj
 * @date 2022/4/26 -9:40
 * ttl 配置文件类代码
 */
@Configuration
public class TtlQueueConfig {
	//普通交换机
	public static final String X_EXCHANGE = "X";
	//普通队列
	public static final String QUEUE_A = "QA";
	public static final String QUEUE_B = "QB";
	//死信交换机
	public static final String Y_DEAD_LETTER_EXCHANGE = "Y";
	//死信队列
	public static final String DEAD_LETTER_QUEUE = "QD";

	public static final String QUEUE_C = "QC";


	//声明xExchange
	@Bean("xExchange")
	public DirectExchange xExchange(){
		return new DirectExchange(X_EXCHANGE);
	}
	// 声明 yExchange
	@Bean("yExchange")
	public DirectExchange yExchange(){
		return new DirectExchange(Y_DEAD_LETTER_EXCHANGE);
	}

	// 声明 queueA
	@Bean("queueA")
	public Queue queueA(){
		Map<String, Object> arguments=new HashMap<>(3);
		//设置死信交换机
		arguments.put("x-dead-letter-exchange", Y_DEAD_LETTER_EXCHANGE);
		//设置死信队列
		arguments.put("x-dead-letter-routing-key", "YD");
		//声明队列ttl
		arguments.put("x-message-ttl",10000);
		return QueueBuilder.durable(QUEUE_A).withArguments(arguments).build();
	}

	// 声明 queueB
	@Bean("queueB")
	public Queue queueB(){
		Map<String, Object> arguments=new HashMap<>(3);
		//设置死信交换机
		arguments.put("x-dead-letter-exchange", Y_DEAD_LETTER_EXCHANGE);
		//设置死信队列
		arguments.put("x-dead-letter-routing-key", "YD");
		//声明队列ttl
		arguments.put("x-message-ttl",40000);
		return QueueBuilder.durable(QUEUE_B).withArguments(arguments).build();
	}

	//死信队列
	@Bean("queueD")
	public Queue queueD(){
		return QueueBuilder.durable(DEAD_LETTER_QUEUE).build();
	}

	//队列A绑定x交换机
	@Bean
	public Binding queueABindingX(@Qualifier("queueA") Queue queueA,
	                              @Qualifier("xExchange") DirectExchange xExchange){
		return BindingBuilder.bind(queueA).to(xExchange).with("XA");
	}

	//队列B绑定x交换机
	@Bean
	public Binding queueBBindingX(@Qualifier("queueB") Queue queueB,
	                              @Qualifier("xExchange") DirectExchange xExchange){
		return BindingBuilder.bind(queueB).to(xExchange).with("XB");
	}

	//声明死信队列 QD 绑定关系
	@Bean
	public Binding deadLetterBindingQAD(@Qualifier("queueD") Queue queueD,
	                                    @Qualifier("yExchange") DirectExchange yExchange){
		return BindingBuilder.bind(queueD).to(yExchange).with("YD");
	}

	//声明队列 C 死信交换机
	@Bean("queueC")
	public Queue queueC(){
		Map<String, Object> args = new HashMap<>(3);
		//声明当前队列绑定的死信交换机
		args.put("x-dead-letter-exchange", Y_DEAD_LETTER_EXCHANGE);
		//声明当前队列的死信路由 key
		args.put("x-dead-letter-routing-key", "YD");
		//没有声明 TTL 属性
		return QueueBuilder.durable(QUEUE_C).withArguments(args).build();
	}
	//声明队列 B 绑定 X 交换机
	@Bean
	public Binding queueCBindingX(@Qualifier("queueC") Queue queueC,
	                              @Qualifier("xExchange") DirectExchange xExchange){
		return BindingBuilder.bind(queueC).to(xExchange).with("XC");
	}



}
